<?php
declare(strict_types=1);

namespace App\Controller;

use App\Service\KsApiService;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\HttpServer\Annotation\AutoController;
use App\Service\RechargeService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Utils\ApplicationContext;
use WeChatPay\Crypto\Rsa;
use WeChatPay\Crypto\AesGcm;
use WeChatPay\Formatter;

#[AutoController]
class UwxController extends BaseController
{
    #[Inject]
    private KsApiService $ksApiService;

    #[Inject]
    private RechargeService $rechargeService;

    public function check_signature(RequestInterface $request, ResponseInterface $response)
    {
        # 验证token问题
//        $applet_id = $request->input("applet_id");
        $all = $request->all();
        $request = ApplicationContext::getContainer()->get(RequestInterface::class);
        $headers = $request->getHeaders();
        $kwaisign = $headers['kwaisign'][0];
        $order_id = $all['data']['out_order_no'];
        $applet_id = $all['data']['attach'];
        var_dump('------------------------支付回调');
        var_dump($all);
        $sign_is_true = $this->ksApiService->get_decrypt_sign($applet_id, json_encode($all), $kwaisign);
        var_dump($sign_is_true);
        if ($sign_is_true) {
            if ($all['biz_type'] == 'PAYMENT') {
                $this->rechargeService->buyback($all, 1);
            }
            return json_encode([
                'result' => 1,
                'message_id' => $all['message_id']
            ]);
        }
    }

}